package com.wutianyi.module.dao.config.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.wutianyi.entity.DataStoreField;
import com.wutianyi.module.commons.config.ConfigDO;
import com.wutianyi.module.commons.config.ConfigTypeEnum;
import com.wutianyi.module.dao.config.ConfigDao;
import com.wutianyi.utils.Utils;

import static com.wutianyi.entity.EntityTypeEnum.Config;
import static com.wutianyi.entity.DataStoreField.*;

/**
 * @author wutianyi
 * 
 */
public class ConfigDaoDataStoreImpl implements ConfigDao {

	private static DatastoreService datastoreService = DatastoreServiceFactory
			.getDatastoreService();

	/**
	 * @throws IOException
	 * 如果没有数据就读取文件进行初始化
	 */
	public void init() throws IOException {
		List<ConfigDO> configs = listConfigsByType(ConfigTypeEnum.COMPANY_STANDARD_PROPERTIES);
		if (CollectionUtils.isEmpty(configs)) {
			InputStream inputStream = ConfigDaoDataStoreImpl.class
					.getResourceAsStream("/data/公司信息组成");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					inputStream));

			DatastoreService dataStoreService = DatastoreServiceFactory
					.getDatastoreService();

			String line = br.readLine();
			int pId = 0;
			List<Entity> entities = new ArrayList<Entity>();
			while (StringUtils.isNotBlank(line)) {
				if (line.startsWith("\t")) {
					ConfigDO config = new ConfigDO();
					config.setName(line.trim());
					config.setpId(pId);
					Entity entity = new Entity(Config.name(), config.hashCode());
					entity.setProperty(DataStoreField.DS_P_TYPE,
							ConfigTypeEnum.COMPANY_STANDARD_PROPERTIES
									.getType());
					entity.setProperty("value", Utils.serialize(config));
					entities.add(entity);
				} else {
					ConfigDO config = new ConfigDO();
					config.setName(line.trim());
					Entity entity = new Entity(Config.name(), config.hashCode());
					entity.setProperty("value", Utils.serialize(config));
					entity.setProperty(DataStoreField.DS_P_TYPE,
							ConfigTypeEnum.COMPANY_STANDARD_PROPERTIES
									.getType());
					entities.add(entity);
					pId = config.hashCode();
				}

				line = br.readLine();
			}
			dataStoreService.put(entities);
		}
	}

	@Override
	public List<ConfigDO> listConfigsByType(ConfigTypeEnum type) {

		Query query = new Query(Config.name());
		query.addFilter(DS_P_TYPE, FilterOperator.EQUAL, 1);

		List<ConfigDO> configs = new ArrayList<ConfigDO>();
		List<ConfigDO> rconfigs = new ArrayList<ConfigDO>();

		Map<Integer, ConfigDO> maps = new HashMap<Integer, ConfigDO>();
		Iterable<Entity> entities = datastoreService.prepare(query)
				.asIterable();
		for (Entity entity : entities) {
			ConfigDO config = (ConfigDO) Utils.unSerialize(entity
					.getProperty(DS_P_VALUE));
			if (null != config) {
				config.setId(config.hashCode());
				if (0 == config.getpId()) {
					maps.put(config.getId(), config);
					rconfigs.add(config);
				} else {
					configs.add(config);
				}
			}
		}
		for (ConfigDO config : configs) {
			ConfigDO parent = maps.get(config.getpId());
			if (null != parent) {
				parent.addChild(config);
			}
		}

		return rconfigs;
	}
}
